﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_MST_DEPARTMENT
// * CreateDate    : 2011/8/5 15:47:49
// * Creater       : CodeSmith
// * LastChangeDate: 2011/8/5 15:47:49
// * LastChanger   : 2011/8/5 15:47:49
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;
using ESOL.Common;

namespace ESOL.Business.Mst
{
    /// <summary>
    /// MstDepartment
    /// </summary>
    public partial class MstDepartmentBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>Entity</returns>
        public static MstDepartmentEntity GetMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.SelectSingle<MstDepartmentEntity>(objMstDepartmentEntity);
        }
        public static MstDepartmentEntity GetMstDepartmentByID(string departmentID)
        {
            if (string.IsNullOrEmpty(departmentID))
            {
                return null;
            }
            MstDepartmentEntity objMstDepartmentEntity = new MstDepartmentEntity();
            objMstDepartmentEntity.DepartmentId = departmentID;
            return DataAccess.SelectSingle<MstDepartmentEntity>(objMstDepartmentEntity);
        }
        public static MstDepartmentEntity GetMstDepartmentByCode(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return null;
            }
            MstDepartmentEntity objMstDepartmentEntity = new MstDepartmentEntity();
            objMstDepartmentEntity.DepartmentCode = code;
            return DataAccess.SelectSingle<MstDepartmentEntity>(objMstDepartmentEntity);
        }
        public static DataTable GetDapartmentByCondition(string condition, string orderBy, string rowCount)
        {
            string sql = CommonFunctions.GetSelect(rowCount) + @"D.*, PD.DEPARTMENT_NAME AS PARENT_NAME, P.NAME_CN AS MANAGER_NAME
                        FROM MST_DEPARTMENT D
                        LEFT JOIN MST_DEPARTMENT PD ON PD.DEPARTMENT_ID = D.PARENT_ID
						LEFT JOIN MST_PERSON P ON P.PERSON_ID = D.MANAGER
                        WHERE 1=1 ";
            if (string.IsNullOrEmpty(condition) == false)
            {
                sql += condition;
            }
            if (string.IsNullOrEmpty(orderBy) == false)
            {
                sql += " ORDER BY " + orderBy;
            }
            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;
        }
        public static List<MstDepartmentEntity> GetDepartmentListAll(bool showDeleted)
        {
            string sql = "";
            if (showDeleted)
            {
                sql = "SELECT * FROM MST_DEPARTMENT";
            }
            else
            {
                sql = "SELECT * FROM MST_DEPARTMENT WHERE DELETE_FLAG=0 ";
            }
            return DataAccess.Select<MstDepartmentEntity>(sql);
        }
        public static List<MstDepartmentEntity> GetRootDepartment()
        {
            string sql = "SELECT * FROM MST_DEPARTMENT WHERE PARENT_ID IS NULL AND DELETE_FLAG != 1";
            return DataAccess.Select<MstDepartmentEntity>(sql);
        }
        public static List<MstDepartmentEntity> GetChildDepartment(string parentID)
        {
            if (string.IsNullOrEmpty(parentID))
            {
                return GetRootDepartment();
            }
            string sql = "SELECT * FROM MST_DEPARTMENT WHERE PARENT_ID = '" + parentID + "' AND DELETE_FLAG != 1";
            return DataAccess.Select<MstDepartmentEntity>(sql);
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.Insert(objMstDepartmentEntity);
        }

        public static bool InsertMstDepartment(List<MstDepartmentEntity> objMstDepartmentEntityList)
        {
            return DataAccess.Insert(objMstDepartmentEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.Update(objMstDepartmentEntity);
        }

        public static bool UpdateMstDepartment(List<MstDepartmentEntity> objMstDepartmentEntityList)
        {
            return DataAccess.Update<MstDepartmentEntity>(objMstDepartmentEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objMstDepartmentEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteMstDepartment(MstDepartmentEntity objMstDepartmentEntity)
        {
            return DataAccess.Delete(objMstDepartmentEntity);
        }

        public static bool DeleteMstDepartment(List<MstDepartmentEntity> objMstDepartmentEntityList)
        {
            return DataAccess.Delete<MstDepartmentEntity>(objMstDepartmentEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetMstDepartmentAll()
        {
            return DataAccess.SelectAllDataSet<MstDepartmentEntity>();
        }
        #endregion
    }


}

